﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;
using System.IO;
namespace VideoWorld.Management
{
    /// <summary>
    /// Summary description for center.
    /// </summary>
    public class ManageVideoInfo : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Panel PanelVideoInfoNavi;
        protected System.Web.UI.WebControls.Panel PanelVideoInfoContent;
        protected System.Web.UI.WebControls.Button ButtonShowVideoInfo;
        protected System.Web.UI.WebControls.Button ButtonAddVideo;
        protected System.Web.UI.WebControls.TextBox TextBoxName;
        protected System.Web.UI.WebControls.TextBox TextBoxFileType;
        protected System.Web.UI.WebControls.TextBox TextBoxMaker;
        protected System.Web.UI.WebControls.TextBox TextBoxLanguage;
        protected System.Web.UI.WebControls.TextBox TextBoxSummary;
        protected System.Web.UI.WebControls.TextBox TextBoxAuthor;
        protected System.Web.UI.WebControls.TextBox TextBoxPerformer;
        protected System.Web.UI.WebControls.Label LabelMessage;
        protected System.Web.UI.WebControls.Panel PanelSearch;
        protected System.Web.UI.HtmlControls.HtmlInputFile FileImage;
        protected System.Data.DataSet dataSetVideo;
        protected System.Web.UI.WebControls.DataGrid dataGridNewsList;
        protected System.Web.UI.WebControls.Button ButtonShowSearch;
        protected System.Web.UI.WebControls.Button ButtonSearch;
        protected System.Web.UI.WebControls.TextBox TextBoxSearchName;
        protected System.Web.UI.WebControls.Image Image;
        protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenImage;
        protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenID;
        protected System.Web.UI.WebControls.DropDownList dropDownListVideoType;
        protected System.Web.UI.WebControls.Panel PanelVideoInfo;
        protected System.Web.UI.WebControls.TextBox textBoxScore;
        protected System.Web.UI.HtmlControls.HtmlInputFile FileTryListen;
        protected string Scroe;
        protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidatorName;
        protected string FileName;
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                SearchVideoVideoType();
                if (Request.QueryString["Action"] != null)
                {
                    if (Request.QueryString["Action"] == "Modify" && Request.QueryString["ID"] != null)
                    {
                        SearchVideoInfo(Request.QueryString["ID"].ToString());
                    }
                }

            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dataSetVideo = new System.Data.DataSet();
            ((System.ComponentModel.ISupportInitialize)(this.dataSetVideo)).BeginInit();
            this.ButtonShowVideoInfo.Click += new System.EventHandler(this.ButtonShowVideoInfo_Click);
            this.ButtonShowSearch.Click += new System.EventHandler(this.ButtonShowSearch_Click);
            this.ButtonAddVideo.Click += new System.EventHandler(this.ButtonAddVideo_Click);
            this.ButtonSearch.Click += new System.EventHandler(this.ButtonSearch_Click);
            this.dataGridNewsList.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dataGridNewsList_PageIndexChanged);
            this.dataGridNewsList.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dataGridNewsList_CancelCommand);
            this.dataGridNewsList.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dataGridNewsList_EditCommand);
            this.dataGridNewsList.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dataGridNewsList_UpdateCommand);
            this.dataGridNewsList.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dataGridNewsList_DeleteCommand);
            // 
            // dataSetVideo
            // 
            this.dataSetVideo.DataSetName = "NewDataSet";
            this.dataSetVideo.Locale = new System.Globalization.CultureInfo("zh-CN");
            this.Load += new System.EventHandler(this.Page_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataSetVideo)).EndInit();

        }
        #endregion

        //添加电影
        private void ButtonAddVideo_Click(object sender, System.EventArgs e)
        {
            if (ButtonAddVideo.Text == "添加")
            {
                SqlParameter[] sparam = new SqlParameter[12];
                sparam[0] = new SqlParameter("@Name", TextBoxName.Text);
                sparam[1] = new SqlParameter("@FileType", TextBoxFileType.Text);
                sparam[2] = new SqlParameter("@Author", TextBoxAuthor.Text);
                sparam[3] = new SqlParameter("@Performer", TextBoxPerformer.Text);
                sparam[4] = new SqlParameter("@Language", TextBoxLanguage.Text);
                sparam[5] = new SqlParameter("@Maker", TextBoxMaker.Text);
                sparam[6] = new SqlParameter("@Summary", TextBoxSummary.Text);
                sparam[7] = new SqlParameter("@Download", SaveFile(FileTryListen, "../TryListen/"));
                sparam[8] = new SqlParameter("@Image", SaveFile(FileImage, "../Images/"));
                sparam[9] = new SqlParameter("@VideoType", dropDownListVideoType.SelectedValue);
                sparam[10] = new SqlParameter("@Score", textBoxScore.Text);
                sparam[11] = new SqlParameter("@RESULT", SqlDbType.Int, 4);
                sparam[11].Direction = ParameterDirection.Output;
                SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["ConnectionString"].ToString(), CommandType.StoredProcedure, "AddVideo", sparam);
                if (Convert.ToInt32(sparam[11].Value) > 0)
                {
                    LabelMessage.Text = "添加成功!";
                }
                else if (Convert.ToInt32(sparam[11].Value) == -1)
                {
                    LabelMessage.Text = "添加失败,系统错误,请重试";
                }
                else if (Convert.ToInt32(sparam[11].Value) == -2)
                {
                    LabelMessage.Text = "添加失败,已经有同名影片";
                }
            }
            else if (ButtonAddVideo.Text == "修改")
            {
                SqlParameter[] sparam = new SqlParameter[13];
                sparam[0] = new SqlParameter("@ID", hiddenID.Value);
                sparam[1] = new SqlParameter("@Name", TextBoxName.Text);
                sparam[2] = new SqlParameter("@FileType", TextBoxFileType.Text);
                sparam[3] = new SqlParameter("@Author", TextBoxAuthor.Text);
                sparam[4] = new SqlParameter("@Performer", TextBoxPerformer.Text);
                sparam[5] = new SqlParameter("@Language", TextBoxLanguage.Text);
                sparam[6] = new SqlParameter("@Maker", TextBoxMaker.Text);
                sparam[7] = new SqlParameter("@Summary", TextBoxSummary.Text);
                sparam[8] = new SqlParameter("@Download", "");


                if (FileImage.PostedFile.FileName.Length > 0)
                {
                    sparam[9] = new SqlParameter("@Image", SaveFile(FileImage, "../Images/"));
                }
                else
                {
                    sparam[9] = new SqlParameter("@Image", hiddenImage.Value);
                }
                sparam[10] = new SqlParameter("@VideoType", dropDownListVideoType.SelectedValue);
                sparam[11] = new SqlParameter("@Score", textBoxScore.Text);
                sparam[12] = new SqlParameter("@RESULT", SqlDbType.Int, 4);
                sparam[12].Direction = ParameterDirection.Output;
                SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["ConnectionString"].ToString(), CommandType.StoredProcedure, "UpdateVideo", sparam);
                if (Convert.ToInt32(sparam[12].Value) > 0)
                {
                    LabelMessage.Text = "更新成功!";
                }
                else if (Convert.ToInt32(sparam[12].Value) == -1)
                {
                    LabelMessage.Text = "更新失败,系统错误,请重试";
                }
                else if (Convert.ToInt32(sparam[12].Value) == -2)
                {
                    LabelMessage.Text = "更新失败,已经有同名影片";
                }
            }
        }


        private void ButtonSearch_Click(object sender, System.EventArgs e)
        {
            dataGridNewsList.CurrentPageIndex = 0;
            SearchVideoInfo();
        }

        private void ButtonShowVideoInfo_Click(object sender, System.EventArgs e)
        {
            PanelSearch.Visible = false;
            PanelVideoInfoContent.Visible = true;
            ButtonAddVideo.Text = "添加";
        }

        private void ButtonShowSearch_Click(object sender, System.EventArgs e)
        {
            PanelSearch.Visible = true;
            PanelVideoInfoContent.Visible = false;
        }

        private void dataGridNewsList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            dataGridNewsList.CurrentPageIndex = e.NewPageIndex;
            SearchVideoInfo();
        }
        ////查询影片类型信息
        private void SearchVideoVideoType()
        {
            DataSet ds = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["ConnectionString"], CommandType.StoredProcedure, "GetVideoType");
            dropDownListVideoType.DataSource = ds;
            dropDownListVideoType.DataValueField = "ID";
            dropDownListVideoType.DataTextField = "Name";
            dropDownListVideoType.DataBind();
        }
        ////查询一个电影的信息
        private void SearchVideoInfo(String ID)
        {

            SqlDataReader VideoInfoReader = SqlHelper.ExecuteReader(ConfigurationSettings.AppSettings["ConnectionString"].ToString(), CommandType.StoredProcedure, "GetVideo", new SqlParameter("@ID", ID));
            int itemIndex = 2;
            while (VideoInfoReader.Read())
            {
                TextBoxName.Text = VideoInfoReader["Name"].ToString();
                FileName = VideoInfoReader["Download"].ToString();
                TextBoxFileType.Text = VideoInfoReader["FileType"].ToString();
                TextBoxAuthor.Text = VideoInfoReader["Author"].ToString();
                TextBoxPerformer.Text = VideoInfoReader["Performer"].ToString();
                TextBoxMaker.Text = VideoInfoReader["Maker"].ToString();
                TextBoxLanguage.Text = VideoInfoReader["Language"].ToString();
                Image.ImageUrl = "../images/" + VideoInfoReader["Image"].ToString();
                TextBoxSummary.Text = VideoInfoReader["Summary"].ToString();
                hiddenImage.Value = VideoInfoReader["Image"].ToString();
                itemIndex = Convert.ToInt32(VideoInfoReader["VideoType"].ToString());
                textBoxScore.Text = VideoInfoReader["Score"].ToString();
                hiddenID.Value = ID;
            }
            ButtonAddVideo.Text = "修改";
            VideoInfoReader.Close();
            this.DataBind();
            dropDownListVideoType.SelectedValue = itemIndex.ToString();
        }
        //上传文件
        private String SaveFile(HtmlInputFile TempFilePath, string filePath)
        {
            if (TempFilePath.PostedFile.FileName.Length != 0)
            {
                String NewFileName = Guid.NewGuid().ToString() + Path.GetExtension(TempFilePath.PostedFile.FileName); //由GUID生成的文件名
                String NewFilePath = Server.MapPath(filePath) + NewFileName;
                try
                {
                    TempFilePath.PostedFile.SaveAs(NewFilePath);							//保存文件到服务器
                }
                catch (Exception errors)
                {
                    Response.Write(errors);
                }
                return NewFileName;
            }
            return "";
        }

        //获得全部热点电影信息
        private void SearchVideoInfo()
        {
            SqlParameter[] sparam = new SqlParameter[2];
            sparam[0] = new SqlParameter("@ID", null);
            if (TextBoxSearchName.Text.Length > 0)
            {
                sparam[1] = new SqlParameter("@Name", TextBoxSearchName.Text);
            }
            else
            {
                sparam[1] = new SqlParameter("@Name", null);
            }

            dataSetVideo = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["ConnectionString"].ToString(), CommandType.StoredProcedure, "GetVideo", sparam);
            dataGridNewsList.DataSource = dataSetVideo;
            dataGridNewsList.DataKeyField = "ID";
            dataGridNewsList.DataBind();

            dataGridNewsList.Visible = true;
            PanelSearch.Visible = true;
            PanelVideoInfoContent.Visible = false;
        }

        private void dataGridNewsList_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            dataGridNewsList.EditItemIndex = e.Item.ItemIndex;
            SearchVideoInfo();
        }

        private void dataGridNewsList_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            dataGridNewsList.EditItemIndex = -1;
            SearchVideoInfo();
        }

        //删除热点影片
        private void dataGridNewsList_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            SqlParameter[] sparam = new SqlParameter[2];
            sparam[0] = new SqlParameter("@ID", dataGridNewsList.DataKeys[e.Item.ItemIndex].ToString());
            sparam[1] = new SqlParameter("@RESULT", SqlDbType.Int, 4);
            sparam[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["ConnectionString"].ToString(), CommandType.StoredProcedure, "DeleteVideo", sparam);
            if (Convert.ToInt32(sparam[1].Value) > 0)
            {
                LabelMessage.Text = "更新成功!";
            }
            else if (Convert.ToInt32(sparam[1].Value) == -1)
            {
                LabelMessage.Text = "更新失败,没有这个影片信息";
            }
            else if (Convert.ToInt32(sparam[1].Value) == -2)
            {
                LabelMessage.Text = "更新失败,系统错误,请重试";
            }
            dataGridNewsList.EditItemIndex = -1;
            SearchVideoInfo();
        }
        //更新影片是否为热点推荐
        private void dataGridNewsList_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            int IsRecommend = 0;


            if (((CheckBox)e.Item.FindControl("CheckBoxIsRecommend")).Checked)
            {
                IsRecommend = 1;
            }

            SqlParameter[] sparam = new SqlParameter[3];
            sparam[0] = new SqlParameter("@ID", dataGridNewsList.DataKeys[e.Item.ItemIndex]);
            sparam[1] = new SqlParameter("@IsRecommend", IsRecommend);
            sparam[2] = new SqlParameter("@RESULT", SqlDbType.Int, 4);
            sparam[2].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings["ConnectionString"].ToString(), CommandType.StoredProcedure, "UpdateRecommendVideos", sparam);
            if (Convert.ToInt32(sparam[2].Value) > 0)
            {
                LabelMessage.Text = "更新成功!";
            }
            else if (Convert.ToInt32(sparam[2].Value) == -1)
            {
                LabelMessage.Text = "更新失败,系统错误";
            }
            dataGridNewsList.EditItemIndex = -1;
            SearchVideoInfo();
        }
    }
}
